import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:sui_shou_pai/module/data.dart';
import 'package:sui_shou_pai/module/effect.dart';
import 'package:sui_shou_pai/news/comment/news_comment_list.dart';
import 'package:sui_shou_pai/news/data/news_list_item_data.dart';

import '../bottom_tool_bar.dart';

class NewsDetail extends StatefulWidget {
  final NewsListItemData item;

  NewsDetail({Key key, this.item}) : super(key: key);

  @override
  NewsDetailState createState() => NewsDetailState();
}

class NewsDetailState extends State<NewsDetail> {
  Function callback;

  bool isLiked = false;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    commentList = NewsCommentList(newsId: widget.item.id);
    callback = (var isRefresh) {
      if (isRefresh) {
        commentList.refreshComment.call(true);
      }
    };
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Column(
        children: <Widget>[
          Expanded(
            child: Container(
              child: ListView(
                children: <Widget>[
                  initHeader(),
                  initContent(),
                ],
              ),
            ),
          ),
          BottomToolBar(
            currentContentType: "news",
            currentContentId: widget.item.id,
            callback: callback,
          )
        ],
      ),
    );
  }

  Widget initHeader() {
    return Stack(
      children: <Widget>[
        initHeaderImage(),
        initHeaderButton(),
      ],
    );
  }

  Widget initHeaderImage() {
    return Container(
        transform: Matrix4.translationValues(0, newsDetailImageOffset, 0),
        child: Image.asset(
          widget.item.headerImagePath,
          height: newsDetailImageHeight,
          width: MediaQuery.of(context).size.width,
          fit: BoxFit.cover,
        ));
  }

  Widget initHeaderButton() {
    return Container(
      padding: EdgeInsets.only(left: 16, right: 16),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          IconButton(
              onPressed: () {
                Navigator.pop(context);
              },
              icon: Icon(
                Icons.arrow_back,
                color: Colors.white,
              )),
          IconButton(
              onPressed: () {},
              icon: Icon(Icons.more_horiz, color: Colors.white))
        ],
      ),
    );
  }

  NewsCommentList commentList;

  Widget initContent() {
    return Container(
      padding: EdgeInsets.all(24),
      transform: Matrix4.translationValues(0, newsDetailTextOffset, 0),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: borderRadiusOnlyLeftTop24(),
      ),
      child: Column(
        children: <Widget>[
          initNewsTitle(),
          initNewsTimeSource(),
          initNewsContent(),
          commentList
        ],
      ),
    );
  }

  Widget initNewsTitle() {
    return Container(
      child: Text(
        widget.item.title,
        style: TextStyle(fontSize: 30, fontWeight: FontWeight.bold),
      ),
    );
  }

  Widget initNewsTimeSource() {
    return Container(
      alignment: Alignment.centerRight,
      margin: EdgeInsets.only(top: 12, bottom: 12),
      child: Text(
        '${widget.item.time}  ${widget.item.source}',
        style: TextStyle(fontSize: 14, color: Colors.grey),
      ),
    );
  }

  Widget initNewsContent() {
    return Container(
      child: Text(
        widget.item.content,
        style: TextStyle(fontSize: 18, height: 1.5),
      ),
    );
  }
}
